package com.mysen.sso.aop.checker;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.mysen.api.CheckBridge;
import com.mysen.json.JSONBridge;
import com.mysen.json.JSONParam;
import com.mysen.sso.cache.SessionCache;

@Component
public class ParamCheckJSONBridge implements CheckBridge{
	
    private static final Logger LOGGER = LoggerFactory.getLogger(ParamCheckJSONBridge.class);

    @Autowired
    private SessionCache sessionCache;
    
	@JSONBridge("checkUnique")
	public Boolean checkUnique(@JSONParam("this") JSONObject config) {
		return Boolean.TRUE;
	}
	
	@JSONBridge("checkUserLogined")
	public Boolean checkUserLogined(@JSONParam("sid") String sid) {
		LOGGER.info("exec checkUserLogined ...");
		//User user = sessionCache.get(sid);
		//return user != null;
		return false;
	}

	@JSONBridge("checkUserRoles")
	public Boolean checkUserRoles(@JSONParam("sid") String sid, @JSONParam("this") JSONObject config) {
		/*LOGGER.info("exec checkUserRoles ... ");
		JSONArray roles = config.getJSONArray("_includeRole");
		if(roles == null || roles.size() == 0) {
			return Boolean.TRUE;
		}
		User user = sessionCache.get(sid);
		Set<Role> roleSet = sessionCache.getRoles(user.getUserId());
		if(roleSet == null) {
			roleSet = new HashSet<>();
		}
		Iterator<Role> iterator = roleSet.iterator();
		while(iterator.hasNext()){
			Role role = iterator.next();
			for(int i=0;i<roles.size();i++){
				if(role.getRoleId().equals(roles.get(i))){
					return Boolean.TRUE;
				}
			}
		}*/
		return Boolean.FALSE;
	}
	
	@JSONBridge("checkUserAny")
	public Boolean checkUserAny() {
		LOGGER.info("exec checkUserAny ...");
		return Boolean.TRUE;
	}
	
	@JSONBridge("checkUserName")
	public Boolean checkUserName() {
		LOGGER.info("exec checkUserName ...");
		return Boolean.TRUE;
	}
}
